package com.dingqb.main.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.dingqb.service.ServiceD;
import com.dingqb.service.ServiceD.myBinder;
import com.dingqb.more.BaseActivity;
import com.dingqb.more.R;
import com.sqlite.SqliteHelper;

public class MainActivity extends BaseActivity implements OnClickListener {
	private final static String ACTION_1 = "com.dingqb.broad";
	private ServiceD.myBinder binder;
	private Intent mIntent;
	private TextView tv_system;
	private TextView tv_multy;
	private TextView tv_sqlite;
	private TextView tv_sqlite_table;
	private TextView tv_animal;

	private TextView tv_webview;
	private String databaseName = "pp.db";
	private SqliteHelper helper;
	private SQLiteDatabase db;
	private ServiceConnection connection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			binder = (myBinder) service;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		// bindService(intent, connection, BIND_AUTO_CREATE);
		mIntent = new Intent(this, ServiceD.class);
		startService(mIntent);

		helper = new SqliteHelper(MainActivity.this, databaseName);
		db = helper.getWritableDatabase();
	}

	private void initView() {
		TextView tv_broadcast = (TextView) findViewById(R.id.tv_broadcast);
		tv_broadcast.setOnClickListener(this);
		TextView tv_notification = (TextView) findViewById(R.id.tv_notification);
		tv_notification.setOnClickListener(this);
		tv_system = (TextView) findViewById(R.id.tv_system);
		tv_system.setOnClickListener(this);
		tv_multy = (TextView) findViewById(R.id.tv_multy);
		tv_multy.setOnClickListener(this);
		tv_sqlite = (TextView) findViewById(R.id.tv_sqlite);
		tv_sqlite.setOnClickListener(this);

		tv_sqlite_table = (TextView) findViewById(R.id.tv_sqlite_table);
		tv_sqlite_table.setOnClickListener(this);
		//
		tv_webview = (TextView) findViewById(R.id.tv_webview);
		tv_webview.setOnClickListener(this);
		//
		tv_animal=(TextView) findViewById(R.id.tv_anima);
		tv_animal.setOnClickListener(this);

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (binder != null) {
			binder.toStartPlay();
			binder.toStopPlay();
			binder.toNext();
			binder.toPrevious();
		} else {
			Timer timer = new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					onResume();
				}
			}, 1000);
		}

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (R.id.tv_notification == id) {
			startActivity(new Intent(this, NotificationActivity.class));
		} else if (id == R.id.tv_system) {
			//TODO  处理各种系统自带界面
			
			// //系统设置界面
			// Intent intent =new Intent (Settings.ACTION_APN_SETTINGS);
			// startActivity(intent);
			// //WIFI 设置界面
			// Intent intent2 =new Intent (Settings.ACTION_WIFI_SETTINGS );
			// startActivity(intent2);

			// //调用系统自带的文件管理器
			// Intent intent3 =new Intent (Intent.ACTION_GET_CONTENT);
			// intent3.setType("*/*");
			// intent3.addCategory(Intent.CATEGORY_OPENABLE);
			// startActivity(intent3);

			// //系统音乐
			// Intent it = new Intent(Intent.ACTION_VIEW);
			// it.setDataAndType(Uri.parse("file://" +
			// Environment.getExternalStorageDirectory() + "/a.MP3"),
			// "audio/MP3");
			// startActivity(it);
			// 系统视频
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(
					Uri.parse(Environment.getExternalStorageDirectory()
							+ "/3.mp4"), "video/mp4");
			startActivity(intent);

		} else if (id == R.id.tv_multy) {

			startActivity(new Intent(MainActivity.this, MultiFragActivity.class));
		} else if (id == R.id.tv_sqlite) {
			//方式一： SqliteOpenHelper

			// 创建ContentValues对象
			ContentValues values = new ContentValues();
			// 向该对象中插入键值对，其中键是列名，值是希望插入到这一列的值，值必须和数据库当中的数据类型一致
			values.put("id", 1);
			values.put("name", "yangyz");
			db.insert("user", null, values);

			//
			String userid = "";
			String name = "";
			Cursor cursor = db.query("user", new String[] { "id", "name" },
					"id=?", new String[] { "1" }, null, null, null);
			// 将光标移动到下一行，从而判断该结果集是否还有下一条数据，如果有则返回true，没有则返回false
			while (cursor.moveToNext()) {
				userid = cursor.getString(cursor.getColumnIndex("id"));
				name = cursor.getString(cursor.getColumnIndex("name"));
			}
			System.out.println("-------------select------------");
			System.out.println("userid: " + userid);
			System.out.println("name: " + name);
			Toast.makeText(MainActivity.this, userid + name, 0).show();
		} else if (id == R.id.tv_sqlite_table) {
			// 方式二
			SQLiteDatabase db = openOrCreateDatabase(databaseName,
					Context.MODE_PRIVATE, null);
			// 创建person表
			db.execSQL("CREATE TABLE IF NOT EXISTS person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");
			Person person = new Person();
			person.name = "john";
			person.age = 30;
			// 插入数据
			db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)", new Object[] {
					person.name, person.age });

			person.name = "david";
			person.age = 33;
			// 查询数据
			Cursor c = db.rawQuery("SELECT * FROM person", null);
			List<Person> list = new ArrayList<MainActivity.Person>();

			while (c.moveToNext()) {
				Person person1 = new Person();
				person1.name = c.getString(c.getColumnIndex("name"));
				person1.age = c.getInt(c.getColumnIndex("age"));
				list.add(person1);
			}
			String age = "";
			String name = "";
			for (Person person2 : list) {
				age = age + "," + person2.getAge();
				name = name + "," + person2.getName();

			}
			Toast.makeText(MainActivity.this, age + name, 0).show();

			// ContentValues以键值对的形式存放数据
			ContentValues cv = new ContentValues();
			cv.put("name", person.name);
			cv.put("age", person.age);
			// 插入ContentValues中的数据
			db.insert("person", null, cv);
			Cursor c2 = db.rawQuery("SELECT * FROM person", null);
			List<Person> list2 = new ArrayList<MainActivity.Person>();
			while (c2.moveToNext()) {
				Person person1 = new Person();
				person1.name = c2.getString(c.getColumnIndex("name"));
				person1.age = c2.getInt(c.getColumnIndex("age"));
				list2.add(person1);
			}

			String age2 = "";
			String name2 = "";
			for (Person person2 : list2) {
				age2 = age2 + "," + person2.getAge();
				name2 = name2 + "," + person2.getName();

			}
			Toast.makeText(MainActivity.this, age2 + name2, 0).show();

		} else if (id == R.id.tv_webview) {
			startActivity(new Intent(this, WebViewActivity.class));
		}
		else if(id==R.id.tv_anima){
			startActivity(new Intent(MainActivity.this,AnimateActivity.class));
		}

		else {
			Intent intent = new Intent(ACTION_1);
			sendBroadcast(intent);
			// myReceriver();
		}

	}

	class Person {
		String name;
		int age;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

	}

	/**
	 * 注册自定义广播接收器
	 */
	private void myReceriver() {
		// 发送广播的action与 IntentFilter里的anction一样则能成功接收到
		Intent intent = new Intent("1234");
		Bundle b = new Bundle();
		b.putInt("i", 11);
		intent.putExtras(b);
		sendBroadcast(intent);

		// 接受广播
		IntentFilter filter = new IntentFilter();
		filter.addAction("1234");

		BroadcastReceiver myRecerive = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				Bundle b = intent.getExtras();
				int i = b.getInt("i");
				System.out.println("动态注册==" + intent.getAction() + "bundle="
						+ i);

			}

		};
		registerReceiver(myRecerive, filter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		stopService(mIntent);
		// Intent.ACTION_CALL;
		// Intent.ACTION_DIAL;

	}

	void mySqlite() {
		String databaseName = "pp.db";
		SqliteHelper helper = new SqliteHelper(MainActivity.this, databaseName);
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("create table user11(id int,name varchar(20))");

	}
}
